<?php

class links_category
{
	var $Links;
	var $mydirname;
	var $controller_name;

	var $nav_html = NULL;
	var $navinfo  = NULL;

	/** Constructor
	 *
	**/
	function links_category($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();
	}

	function index($cid = 0, $field = NULL, $order = NULL, $offset = 0)
	{
		if (empty($cid) || empty($this->Links->allcid[$cid])) {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_NO_CATEGORY);
			exit();
		}

		$this->Links->setWhere('status', 0, '>');
		$this->Links->setWhere('cid', "%[$cid]%", 'LIKE');
		$this->Links->setWhere('date', time(), '<');
		$numrows = $this->Links->getCount();

		$links = array();
		$lang_cursortedby = $order_html = '';
		if ($numrows > 0) {
			$offset = intval($offset);
			$this->_set_navigation($cid, $field, $order, $offset, $numrows);

			$this->Links->clearWhere();
			$this->Links->setWhere('status', 0, '>');
			$this->Links->setWhere('cid', "%[$cid]%", 'LIKE');
			$this->Links->setWhere('l.date', time(), '<');
			$this->Links->setOrder($this->order_by);

			$links            = $this->Links->getDatas($this->Links->per_page, $offset);
			$lang_cursortedby = sprintf(_MD_CURSORTEDBY, $this->Links->convertOrder($this->order_by));
			$order_html       = $this->Links->makeOrderHtml($this->controller_name, 'index', $this->order_by, 0, $cid);
		}

		$bread = $this->Links->getBreadcrumbs();
	//	$breadcrumbs_base_url = $this->_base_url_for_breadcrumbs();
		$breadplus = assign_get_breadcrumbs_by_tree($this->Links->table_cat, 'cid', 'pid', 'title', $cid, $this->_base_url_for_breadcrumbs());
		$bread = array_merge($bread, $breadplus);

		$data = array(
			'category_id'       => $cid,
			'categories'        => $this->Links->Categories->getCategories($cid) ,
			'links'             => $links,
			'lang_cursortedby'  => $lang_cursortedby,
			'lang_thereare'     => sprintf(_MD_CATEGORYIS, $numrows),
			'order_html'        => $order_html,
			'nav'               => $this->nav_html,
			'navinfo'           => $this->navinfo,
			'xoops_breadcrumbs' => $bread,
			'xoops_pagetitle'   => $this->Links->module_name ." : ". $bread[count($bread)-1]['name'],
		);

		$template = $this->Links->getTemplatePrefix() . '_category.html';
		$this->Links->view($template, $data);

	}

	// For Mobile
	function tree()
	{
		if (! $this->Links->is_mobile) {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 3, 'MOBILE only');
		}

		$bread = $this->Links->getBreadcrumbs();
		$bread[] = array('name' => _MD_CATEGORY_LIST);

		$data = array(
			'categories'        => $this->Links->Categories->getCategories(),
			'xoops_breadcrumbs' => $bread,
		);

		$template = $this->Links->getTemplatePrefix() . '_category_tree.html';
		$this->Links->view($template, $data);
	}

	function _set_navigation($cid = 0, $field = NULL, $order = NULL, $offset = 0, $numrows = 0)
	{
		if(	!empty($field) && !empty($order) 
					&& in_array(strtolower($field), array('title', 'date', 'rating', 'hits', 'access'))
					&& in_array(strtoupper($order), array('ASC', 'DESC'))
				) {
			$field = strtolower($field);
			$order = strtoupper($order);
		} else {
			$field = 'date';
			$order = 'DESC';
		}
		$this->order_by = $field . ' ' . $order;

		if ($offset >= $numrows) $offset = 0 ;
		if ($numrows > $this->Links->per_page) {
			// Navigation
			$extra_arg = array(
					'cid' => $cid,
					'f'   => $field,
					'ord' => $order,
					'p'   => $offset,
					'c'   => $this->controller_name,
					'm'   => 'index'
				);

			$nav = new links_PageNav($numrows, $this->Links->per_page, $extra_arg, $this->Links->uri_option_arr, $this->mydirname);
			$this->nav_html = $nav->renderNav(7) ;

			$last = $offset + $this->Links->per_page;
			if( $last > $numrows ) $last = $numrows;
			$this->navinfo = sprintf(_MD_LINKS_NAVINFO, $offset + 1, $last, $numrows);
		}
	}

	function _base_url_for_breadcrumbs()
	{
		$uri = $this->Links->baseUrl();

		if ($this->Links->module_config['url_approach'] > 0) {
			//segment approach
			$uri .= 'category/index/%u' ;
			if (!empty($this->Links->module_config['url_suffix'])) {
				$uri .= $this->Links->module_config['url_suffix'];
			}
		} else {
			//query approach
			$uri .= 'index.php?c=category&amp;cid=%u' ;
		}

		return $uri;
	}


}

